package org.kunteng.yunac;

import org.kunteng.yunac.entity.MerchantNumStatic;
import org.kunteng.yunac.entity.PVUV;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2015/12/3.
 */
public class DBRecordGenerator {
    public static DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    public static List<String> ChannelIDSource = Arrays.asList(new String[]{
            "1449133311231","1449193611895","1449196646712","1449196851949"
    });
    public static List<String> MerchantIdSource = Arrays.asList(new String[]{"1449196851949"});

    private Map records = new HashMap();
    private Map pfRecords = new HashMap();

    public String getChannelID() {
        return ChannelIDSource.get((int)Math.floor(Math.random() * ChannelIDSource.size()));
    }

    public String getMerchantID() {
        return MerchantIdSource.get((int)Math.floor(Math.random() * MerchantIdSource.size()));
    }

    public MerchantNumStatic generateOneMerchantNumStatic(String startDate, int day) throws ParseException {
        MerchantNumStatic numStatic = new MerchantNumStatic();
        Date d = new Date();

        String key = null;
        do {
            d.setTime((long) Math.floor(Math.random() * day * 24 * 60 * 60 * 1000) + df.parse(startDate).getTime());
            numStatic.setDate(df.format(d));
            numStatic.setMid(getChannelID());
        } while (pfRecords.containsKey(numStatic.hashCode()));

        numStatic.setIncrement((int) Math.floor(Math.random() * 200));
        numStatic.setOld((int) Math.floor(Math.random() * 200));
        numStatic.setJindian((int) Math.floor(Math.random() * 200));
        numStatic.setLiushi((int) Math.floor(Math.random() * 200));
        numStatic.setLuren((int) Math.floor(Math.random() * 200));

        pfRecords.put(numStatic.hashCode(), numStatic);
        return numStatic;
    }

    public PVUV generateOnePVUV(String startDate, int day) throws ParseException {
        PVUV pvuv = new PVUV();
        Date d = new Date();

        String key = null;
        do {
            d.setTime((long) Math.floor(Math.random() * day * 24 * 60 * 60 * 1000) + df.parse(startDate).getTime());
            pvuv.setDate(df.format(d));
            pvuv.setChannelId(getChannelID());
        } while (records.containsKey(pvuv.hashCode()));

        pvuv.setPv((int) Math.floor(Math.random() * 200));
        pvuv.setUv((int) Math.floor(Math.random() * 200));

        records.put(pvuv.hashCode(), pvuv);
        return pvuv;
    }

    public void generatePVUVSQL(String fileName, String startDate, int day, int recordNum) throws IOException, ParseException {
        int maxRecordNum = ChannelIDSource.size() * day;
        if (recordNum > maxRecordNum) {
            recordNum = maxRecordNum;
            System.out.println("Max record number : " + maxRecordNum);
        }

        if (records == null) {
            records = new HashMap();
        }
        records.clear();

        FileWriter fw = new FileWriter(fileName);
        fw.append("delete from pvuv").append(";\n").append("insert into pvuv values ");
        for (int i = 0; i < recordNum; i++) {
            if (i!=0) {
                fw.append(',');
            }
            PVUV pvuv = generateOnePVUV(startDate, day);
            fw.append("('").append(pvuv.getDate()).append("','").append(pvuv.getChannelId()).append("',").append(String.valueOf(pvuv.getPv()))
                    .append(',').append(String.valueOf(pvuv.getUv())).append(")");
        }
        fw.append(";\n");
        fw.flush();
        fw.close();
    }

    public void generateFPCountDaySQL(String fileName, String startDate, int day, int recordNum) throws IOException, ParseException {
        int maxRecordNum = ChannelIDSource.size() * day;
        if (recordNum > maxRecordNum) {
            recordNum = maxRecordNum;
            System.out.println("Max record number : " + maxRecordNum);
        }

        if (pfRecords == null) {
            pfRecords = new HashMap();
        }
        pfRecords.clear();

        FileWriter fw = new FileWriter(fileName);
        fw.append("delete from pf_count_day").append(";\n")
                .append("insert into pf_count_day(salerId,luren_num,liushi_num,jindian_num,new_num,old_num,date) values ");
        for (int i = 0; i < recordNum; i++) {
            if (i!=0) {
                fw.append(',');
            }
            MerchantNumStatic numStatic = generateOneMerchantNumStatic(startDate, day);
            fw.append("('").append(numStatic.getMid()).append("',").append(String.valueOf(numStatic.getLuren())).append(",").append(String.valueOf(numStatic.getLiushi()))
                    .append(',').append(String.valueOf(numStatic.getJindian())).append(",")
                    .append(String.valueOf(numStatic.getIncrement())).append(",")
                    .append(String.valueOf(numStatic.getOld())).append(",'")
                    .append(numStatic.getDate()).append("')");
        }
        fw.append(";\n");
        fw.flush();
        fw.close();
    }
}
